package com.yf.exam.modules.repo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yf.exam.core.api.ApiRest;
import com.yf.exam.core.api.controller.BaseController;
import com.yf.exam.core.api.dto.PagingReqDTO;
import com.yf.exam.modules.repo.dto.request.PublicRepoReqDTO;
import com.yf.exam.modules.repo.dto.response.RepoRespDTO;
import com.yf.exam.modules.repo.service.RepoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 公共题库控制器
 * </p>
 *
 * @author 聪明笨狗
 * @since 2025-09-30
 */
@Api(tags = {"公共题库"})
@RestController
@RequestMapping("/exam/api/public-repo")
public class PublicRepoController extends BaseController {

    @Autowired
    private RepoService repoService;

    /**
     * 公共题库分页查找
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "公共题库分页查找")
    @RequestMapping(value = "/paging", method = { RequestMethod.POST})
    public ApiRest<IPage<RepoRespDTO>> paging(@RequestBody PagingReqDTO<PublicRepoReqDTO> reqDTO) {
        // 转换参数类型
        PagingReqDTO<com.yf.exam.modules.repo.dto.request.RepoReqDTO> convertReq = new PagingReqDTO<>();
        convertReq.setSize(reqDTO.getSize());
        convertReq.setCurrent(reqDTO.getCurrent());
        
        com.yf.exam.modules.repo.dto.request.RepoReqDTO params = new com.yf.exam.modules.repo.dto.request.RepoReqDTO();
        if (reqDTO.getParams() != null) {
            params.setTitle(reqDTO.getParams().getTitle());
            params.setSchoolId(reqDTO.getParams().getSchoolId());
            params.setSubject(reqDTO.getParams().getDepartType());
        }
        params.setOnlyPublic(true);
        convertReq.setParams(params);
        
        //分页查询并转换
        IPage<RepoRespDTO> page = repoService.paging(convertReq);
        return super.success(page);
    }
}